import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class test3 {
    public static void main(String[] args) {
        //定义相关变量
        int[] userRedBall = new int[6];//用户选择的红色球号码
        int[] sysRedBall = new int[6];//系统生成的红色球号码
        int userBlueBall = 0;//用户选择的蓝球
        int sysBlueBall = 0;
        int redCount = 0;//记录用户选择正确的红色球的个数
        int blueCount = 0;
        int[] redBall = new int[33];//用于存储1-33的红色球的号码

        //填充红色数组
        for (int i = 0; i < redBall.length; i++) {
            redBall[i] = i+1;//存储1-33
        }

        //游戏开始
        System.out.println("双色球游戏开始，Good luck! ");
        System.out.println("请问您是选择机选还是手选（1.机选，2.手选）：");

        Scanner scan = new Scanner(System.in);
        Random ran = new Random();
        boolean flag = true;
        while(flag){
            int isAuto = scan.nextInt();
            switch(isAuto){
                //机选
                case 1:
                    computerSeletion(redBall,userRedBall);//红球
                    userBlueBall = ran.nextInt(16)+1;//蓝球
                    flag = false;
                    break;
                //手选
                case 2:
                    System.out.println("请输入您选择的红球号码(1-33):");
                    for (int i = 0; i < userRedBall.length; i++) {
                        userRedBall[i] = scan.nextInt();
                    }
                    System.out.println("请输入您选择的蓝球号码(1-16):");
                    userBlueBall = scan.nextInt();
                    flag = false;
                    break;
                default:
                    System.out.println("请问您是选择机选还是手选（1.机选，2.手选）：");
                    break;
            }
        }

        //系统随机生成号码
        //红球
        computerSeletion(redBall,sysRedBall);
        //蓝球
        sysBlueBall = ran.nextInt(16)+1;

        //统计结果
        //统计红球
        for (int i = 0; i < userRedBall.length-redCount; i++) {
            for (int j = 0; j < sysRedBall.length; j++) {
                if(userRedBall[i]==sysRedBall[j]){
                    int temp = sysRedBall[j];
                    sysRedBall[j] = sysRedBall[sysRedBall.length-1-redCount];
                    sysRedBall[sysRedBall.length-1-redCount] = temp;
                    redCount++;
                }
            }
        }
        //统计蓝球
        if(userBlueBall==sysBlueBall){
            blueCount = 1;
        }

        //验证是否中奖
        if(blueCount==0 && redCount <= 3){
            System.out.println("抱歉，您没有中奖!");
        }else if(blueCount==1 && redCount<3){
            System.out.println("恭喜你中了六等奖，5块钱！");
        }else if((blueCount==1 && redCount==3) || (blueCount==0 && redCount==4)){
            System.out.println("恭喜你中了五等奖，10块钱！");
        }else if((blueCount==1 && redCount==4) || (blueCount==0 && redCount==5)){
            System.out.println("恭喜你中了四等奖，300块钱！");
        }else if(blueCount==1 && redCount==5){
            System.out.println("恭喜你中了三等奖，1000块钱！");
        }else if(blueCount==0 && redCount==6){
            System.out.println("恭喜你中了二等奖，15w！");
        }else if(blueCount==1 && redCount==6){
            System.out.println("恭喜你中了一等奖，500w！");
        }else{
            System.out.println("系统有误，中奖无效！");
        }

        //公布结果
        System.out.println("本期中奖红球号码为：");
        sort(sysRedBall);
        System.out.println(Arrays.toString(sysRedBall));
        System.out.println("本期中奖蓝球号码为："+sysBlueBall);

        System.out.println("您选择的红球号码是：");
        sort(userRedBall);
        System.out.println(Arrays.toString(userRedBall));
        System.out.println("您选择的蓝球号码是："+userBlueBall);

        System.out.println("买双色球，造福你我他。谢谢!");
    }

    //冒泡排序
    public static void sort(int[] ball){
        for (int i = 0; i < ball.length-1; i++) {
            for (int j = 0; j < ball.length-1-i; j++) {
                if(ball[j]>ball[j+1]){
                    ball[j] = ball[j] + ball[j+1];
                    ball[j+1] = ball[j] - ball[j+1];
                    ball[j] = ball[j] - ball[j+1];
                }
            }
        }
    }
    //用于在指定的数组中，随机生成多个不重复的数据（算法）

    public static void computerSeletion(int[] redBall,int[] userRedBall){
        Random ran = new Random();
        int index = -1;
        for (int i = 0; i < userRedBall.length; i++) {
            index = ran.nextInt(redBall.length-i);
            userRedBall[i] = redBall[index];
            int temp = redBall[index];

            redBall[index] = redBall[redBall.length-1-i];
            redBall[redBall.length-1-i] = temp;
        }
    }
}





